#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

// 定义雷区大小
const int rols = 10;
const int cols = 10;
// 定义雷的数量
const int NUM_MINES = 10;

/**
 * @param gameBoard 界面数组界面
 * @param mineBoard 地雷数组界面
 */

// 初始化数组
void initialise(char gameBoard[rols][cols], char mineBoard[rols][cols])
{
    for (int i = 0; i < rols; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            gameBoard[i][j] = '*';
            mineBoard[i][j] = '0';
        }
    }
}

// 布雷
void arrange(char mineBoard[rols][cols])
{
    srand(time(NULL));
    int minesPlaced = 0;
    while (minesPlaced < NUM_MINES)
    {
        int row = rand() % rols;
        int col = rand() % cols;
        if (mineBoard[row][col] == '0')
        {
            mineBoard[row][col] = '#';
            minesPlaced++;
        }
    }
}

// 计算某个位置周围的雷的数量
int number_mine(char mineBoard[rols][cols], int row, int col)
{
    int count = 0;
    for (int i = row - 1; i <= row + 1; i++)
    {
        for (int j = col - 1; j <= col + 1; j++)
        {
            if (i >= 0 && i < rols && j >= 0 && j < cols && mineBoard[i][j] == '#')
            {
                count++;
            }
        }
    }
    return count;
}

// 探索操作
void explore(char gameBoard[rols][cols], char mineBoard[rols][cols], int row, int col, int rols, int cols)
{
    if (mineBoard[row][col] == '#')
    {
        std::cout << "Game Over" << std::endl;
        for (int  i = 0; i < rols; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                gameBoard[i][j] = mineBoard[i][j];
            }
            
        }
        return;
    }
    int numMines =  mineBoard[rols][cols];
    gameBoard[row][col] = numMines + '0';
}

// 标记雷区
void mark_Mine(char gameBoard[rols][cols], int row, int col)
{
    if (gameBoard[row][col] == '*')
    {
        gameBoard[row][col] = 'F' + '0';
    }
}

// 取消标记
void unmark_Mine(char gameBoard[rols][cols], int row, int col)
{
    if (gameBoard[row][col] == 'F')
    {
        gameBoard[row][col] = '*';
    }
}

// 打印游戏界面
void printBoard(char board[rols][cols])
{
    for (int i = 0; i < rols; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            if (board[i][j] == '#')
            {
                std::cout << "# ";
            }
            
            else
            {
                int numMines = number_mine(board, i, j);
                std::cout << numMines << " ";
            }
        }
        std::cout << std::endl;
    }
}
void printBoardFule(char board[rols][cols])
{
    for (int i = 0; i < rols; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << board[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

int main()
{
    char gameBoard[rols][cols];
    char mineBoard[rols][cols];
    initialise(gameBoard, mineBoard);
    arrange(mineBoard);

    int markedCount = 0;
    int correct_Marked_Count = 0;

    while (true)
    {
        std::cout << "游戏开始: " << std::endl;
        printBoard(mineBoard);
        cout << endl;
        printBoardFule(gameBoard);
        std::cout << "输入行，列 和动作(e: 探索, f: 标记, u: 取消标记): ";
        std::cout << std::endl;

        int row, col;
        char action;
        std::cin >> row >> col >> action;
        if (row < 0 || row >= rols || col < 0 || col >= cols)
        {
            std::cout << "输入的行或列超出范围，请重新输入" << std::endl;
            continue;
        }
        if (action == 'e')
        {
            gameBoard[row][col] = mineBoard[row][col];

            explore(gameBoard, mineBoard, row, col, rols, cols);
        }
        else if (action == 'f')
        {
            mark_Mine(gameBoard, row, col);
            markedCount++;
            if (mineBoard[row][col] == '#')
            {
                correct_Marked_Count++;
            }
        }
        else if (action == 'u')
        {
            unmark_Mine(gameBoard, row, col);
            markedCount--;
        }

        cout << "correct_Marked_Count : " << correct_Marked_Count <<
            ", markedCount : " << markedCount  << 
            ", NUM_MINES : " << NUM_MINES << endl;

        if (correct_Marked_Count == NUM_MINES && markedCount == NUM_MINES)
        {
            std::cout << "You Win！！！！！" << std::endl;
            break;
        }
    }
    return 0;
}

